package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.Theme;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-10-13 09:38:00
 */
@Component
public class ThemeSpecifications {

    public static Specification<Theme> queryList(Theme theme) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (theme.getId() != null) {
                predicates.add(builder.like(root.get("id" ).as(String.class), "%" + theme.getId() + "%" ));
            }
            if (theme.getTheme() != null) {
                predicates.add(builder.like(root.get("theme" ).as(String.class), "%" + theme.getTheme() + "%" ));
            }
            if (theme.getIsColorChanged() != null) {
                predicates.add(builder.like(root.get("isColorChanged" ).as(String.class), "%" + theme.getIsColorChanged() + "%" ));
            }
            if (theme.getImage() != null) {
                predicates.add(builder.like(root.get("image" ).as(String.class), "%" + theme.getImage() + "%" ));
            }
            if (theme.getIsEnabled() != null) {
                predicates.add(builder.like(root.get("isEnabled" ).as(String.class), "%" + theme.getIsEnabled() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
